import Vue from 'vue';
import router from '../../../src/router';
import bus from '../../../static/js/bus.js';
import VueResource from 'vue-resource';
import NProgress from 'nprogress'//进度条
import 'nprogress/nprogress.css'

NProgress.configure({
	easing: 'ease',  // 动画方式
	speed: 500,  // 递增进度条的速度
	showSpinner: false, // 是否显示加载ico
	trickleSpeed: 200, // 自动递增间隔
	minimum: 0.3 ,// 初始化时的最小百分比
})

Vue.use(VueResource)
import {
	Loading
} from 'element-ui'; //菊花

let service_url=null

startApp()
//获取配置文件
async function startApp() {
	console.log('当前运行环境',process.env.NODE_ENV);
	if(process.env.NODE_ENV=='development'){
		let data=await Vue.http.get('/static/config.json')
		service_url=JSON.parse(data.bodyText).DEV_URL
	}else{
		service_url='http://135.0.64.217:8080/sealTransferApi'
		// let data=await Vue.http.get('./static/config.json')
		// service_url=JSON.parse(data.bodyText).BUILD_URL
	}
	return service_url
}
console.log(service_url);


async function doGet(url, req, successCallback, errorCallback) {
	console.log(service_url);
	// let service_url=await startApp()
	req=filterUndefined(req)
	// 每次切换页面时，调用进度条
	NProgress.start();
	return new Promise((result,reject)=>{
		Vue.http.get(service_url + url,{params:req}, {
			emulateJSON: true, //允许跨越
		}).then(function(response) {
			if(response.data.code==10019){
				//未登录,跳转登录页面
				Vue.prototype.$message({
					message: response.data.message,
					type: 'warning'
				});
				router.push({name: 'login',path: '/login'})
			}
			// 在即将进入新的页面组件前，关闭掉进度条
			NProgress.done()
			result(response.data)
			// 响应成功回调
			successCallback && successCallback(response.data);
		}, function(error) {
			// 在即将进入新的页面组件前，关闭掉进度条
			NProgress.done()
			// 响应错误回调
			reject(error)
			errorCallback && errorCallback(error)
		});
	})
	
}
async function doDelete(url, req, successCallback, errorCallback) {
	// let service_url=await startApp()
	req=filterUndefined(req)
	// 每次切换页面时，调用进度条
	NProgress.start();
	return new Promise((result,reject)=>{
		Vue.http.delete(service_url + url,{params:req}, {
			emulateJSON: true, //允许跨越
			_timeout: 6000,
			onTimeout: function(request) {
				Vue.prototype.$message({
					message: '请求超时',
					type: 'warning'
				});
			}
		}).then(function(response) {
			if(response.data.code==10019){
				//未登录,跳转登录页面
				Vue.prototype.$message({
					message: response.data.message,
					type: 'warning'
				});
				router.push({name: 'login',path: '/login'})
			}
			// 在即将进入新的页面组件前，关闭掉进度条
			NProgress.done()
			result(response.data)
			// 响应成功回调
			successCallback && successCallback(response.data);
		}, function(error) {
			// 在即将进入新的页面组件前，关闭掉进度条
			NProgress.done()
			// 响应错误回调
			reject(error)
			errorCallback && errorCallback(error)
		});
	})
}
async function doPost(url, req, successCallback, errorCallback) {
	// let service_url=await startApp()
	req=filterUndefined(req)
	// 每次切换页面时，调用进度条
	NProgress.start();
	return new Promise((result,reject)=>{
		Vue.http.post(service_url + url,req, {
			emulateJSON: true, //允许跨越
			_timeout: 6000,
			onTimeout: function(request) {
				Vue.prototype.$message({
					message: '请求超时',
					type: 'warning'
				});
			}
		}).then(function(response) {
			if(response.data.code!=0){
				//未登录,跳转登录页面
				Vue.prototype.$message({
					message: response.data.message,
					type: 'warning'
				});
			}
			if(response.data.code==10019){
				//未登录,跳转登录页面
				Vue.prototype.$message({
					message: response.data.message,
					type: 'warning'
				});
				router.push({name: 'login',path: '/login'})
			}
			// 在即将进入新的页面组件前，关闭掉进度条
			NProgress.done()
			result(response.data)
			// 响应成功回调
			successCallback && successCallback(response.data);
		}, function(error) {
			// 在即将进入新的页面组件前，关闭掉进度条
			NProgress.done()
			// 响应错误回调
			reject(error)
			errorCallback && errorCallback(error)
		});
	})
}
// //限制上传文件格式第一个传上传文件名，第二个限制类型
function restrictFormatUploaded(name, types) {
	let mun = name.indexOf('.')
	let sliceName = name.slice(mun, name.length)
	// let filetypes =["jpg","jpeg","png","rar","txt","zip","doc","ppt","xls","pdf","docx","xlsx","JPG","JPEG","PNG","RAR","TXT","ZIP","DOC","PPT","XLS","PDF","DOCX","XLSX"];
	let filetypes = types
	let arr = filetypes.filter(function(res) {
		if (sliceName.includes(res)) {
			return res;
		}
	})
	let onoff = false
	if (arr.length > 0) {
		onoff = true
	} else {
		onoff = false
	}
	return onoff;
}

// //获取时间
function getBeforeDate(n) { //n为你要传入的参数，当前为0，前一天为-1，后一天为1
	let date = new Date();
	let year, month, day, s;
	date.setDate(date.getDate() + n);
	year = date.getFullYear();
	month = date.getMonth() + 1;
	day = date.getDate();
	s = year + '/' + (month < 10 ? ('0' + month) : month) + '/' + (day < 10 ? ('0' + day) : day);
	return s;
}
//过滤传参中的undefined
function filterUndefined(req){
	let {keys,values,entries} =Object
	for( let [keys,values] of entries(req)){
		if(values=='undefined'||values==undefined){
			req[keys]=''
		}
	}
	return req
}
//格式化时间
function formatDate(data){
	let date = new Date(data);
	let str=date.getFullYear()+'-'+(date.getMonth() + 1)+'-'+date.getDate()+' '+date.getHours()+':'+(date.getMinutes()<10?'0'+date.getMinutes():date.getMinutes())+':'+(date.getSeconds()<10?'0'+date.getSeconds():date.getSeconds())
	return str
}

import websocket from '../function/websocket.js'
import RSA from '../function/RSA.js'
const $lizhao={
	websocket,
	RSA
}
Vue.prototype.$lizhao=$lizhao

export default {
	doGet: doGet,
	doPost: doPost,
	doDelete:doDelete,
	getBeforeDate: getBeforeDate,
	restrictFormatUploaded: restrictFormatUploaded,
	formatDate:formatDate,
	startApp:startApp,
}
